# -*- coding: utf-8 -*-
'''
Created on Apr 3, 2012

@author: LONG HOANG GIANG
'''

import sys, os
sys.path.append(os.path.expanduser('/home5/vietcntt/longhoanggiang/python'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import commonlib
from urlparse import urljoin
import gzip
import simplejson as json
import datetime

cookie = '''manga24h.com    FALSE    /    FALSE    0    PHPSESSID    aiuj8i3crjj1i09vjc19ng2kj2
manga24h.com    FALSE    /    FALSE    1356426024    popads    1
manga24h.com    FALSE    /    FALSE    1356469182    Ads    Ads%20PopUnder
manga24h.com    FALSE    /81/    FALSE    0    tapchi    0'''


def getChapter(url):
    print 'getChapter url = {0}'.format(url)
#    tree = Http.getXMLTree(url, cookie=cookie)
    tree = commonlib.loadweb(url, cookie=cookie).build_tree()
    name = commonlib.stringify(tree.xpath("//span[@class='manga_name']"))
    images = []
    for item in tree.xpath("//ul[@id='portfolio']/li/img"):
        src = commonlib.normalize_str(item.get('src', ''))
        if src != '':
            src = urljoin('url', src)
            images.append(src)
            print src
    return {'chapter': name, 'images': images}


def getListLink(output):
    url = 'http://manga24h.com/47894/ONE-PIECE-Dao-Hai-Tac-Chapter-663/'
#    tree = Http.getXMLTree(url, cookie=cookie)
    tree = commonlib.loadweb(url, cookie=cookie).build_tree()
    data = []
    for item in tree.xpath("//select[@name='chapters']/option"):
        link = item.get('value', '')
        if link != '':
            link = urljoin('http://manga24h.com', link)
            data.append(getChapter(link))
    if not os.path.exists(os.path.dirname(output)): os.makedirs(os.path.dirname(output), 0777)
    fp = gzip.open(output, 'wb')
    jsondata = json.dumps(data)
    fp.write(jsondata)
    fp.close()
    
        
if __name__ == '__main__':
        
    #getChapter('http://manga24h.com/1884/Than-Dong-Dat-Viet-Tap-98/')     
       
    getListLink("/longhoanggiang/manga24h/{0}/output.gz".format("one-piece"))
    print '>> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)